package com.yifang.yibingproject.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Lew
 * @description：
 * @date ：Created in 2022/2/16 17:00
 */
@ConditionalOnProperty(name = "swagger.enabled", havingValue = "true")
@Configuration
@EnableSwagger2
@Controller
public class SwaggerConfig {

    /**
     * swagger请求
     * @return
     */
    @GetMapping("s")
    public String redirectToSwaggerHtml() {
        return "redirect:/swagger-ui.html";
    }

    @Bean
    public Docket createRestApi(){
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name("Authorization")
                .description("token")
                .modelRef(new ModelRef("String"))
                .parameterType("header")
                .required(false).build();
        parameters.add(parameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                //TODO  这里写下你的个性化配置
                .apiInfo(apiInfo("网页端接口","后端化接口实现"))
                .groupName("网页端接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yifang.yibingproject.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }


    private ApiInfo apiInfo(String name,String description){
        return new ApiInfoBuilder()
                .title("后端接口 - " + name)
                .description(description)
                .version("1.0.0")
                //TODO 写下你的个性化配置
                .contact(new Contact("罗伟-软件开发","","459267841@qq.com"))
                .build();
    }
}
